﻿using Apps.Common;
using Apps.Models;
using System.Linq;
using System.Collections.Generic;
using Apps.Models.PUB;

namespace Apps.BLL.PUB
{
    public partial class PUB_DEVICEMODELBLL
    {

        public override List<PUB_DEVICEMODELModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            IQueryable<PUB_DEVICEMODEL> queryData = null;
            string pid = parentId.ToString();
            if (pid != "0")
            {
                queryData = m_Rep.GetList(a => a.PUB_DEVICETYPE.TYPE_ID == pid);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
                            a => (
                                    a.REMARKS.Contains(queryStr)
                                   || a.MODEL_NAME.Contains(queryStr)
                                   || a.TYPE_ID.Contains(queryStr)
                                   || a.FACTURER_ID.Contains(queryStr)
                                   || a.MODEL_ID.Contains(queryStr)
                                 )
                            );
            }
            pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
        public override List<PUB_DEVICEMODELModel> CreateModelList(ref IQueryable<PUB_DEVICEMODEL> queryData)
        {

            List<PUB_DEVICEMODELModel> modelList = (from r in queryData
                                                    select new PUB_DEVICEMODELModel
                                                    {
                                                        REMARKS = r.REMARKS,
                                                        MODEL_NAME = r.MODEL_NAME,
                                                        TYPE_ID = r.TYPE_ID,
                                                        FACTURER_ID = r.FACTURER_ID,
                                                        MODEL_ID = r.MODEL_ID,
                                                        MANUFACTURERName = r.PUB_MANUFACTURER.FACTURER_NAME,
                                                        DEVICETYPEName = r.PUB_DEVICETYPE.TYPE_NAME,
                                                    }).ToList();
            return modelList;
        }
        public override PUB_DEVICEMODELModel GetById(object id)
        {
            if (IsExists(id))
            {

                PUB_DEVICEMODEL entity = m_Rep.GetById(id);
                PUB_DEVICEMODELModel model = new PUB_DEVICEMODELModel();
                model.REMARKS = entity.REMARKS;
                model.MODEL_NAME = entity.MODEL_NAME;
                model.TYPE_ID = entity.TYPE_ID;
                model.FACTURER_ID = entity.FACTURER_ID;
                model.MODEL_ID = entity.MODEL_ID;
                model.MANUFACTURERName = entity.PUB_MANUFACTURER.FACTURER_NAME;
                model.DEVICETYPEName = entity.PUB_DEVICETYPE.TYPE_NAME;
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}

